home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pcl4c341.zip / XYMODEM.C < prev    next >
Text File  |  1992-12-24  |  7KB  |  227 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys\types.h>
  4. #include <sys\stat.h>
  5.  
  6. #include "pcl4c.h"
  7. #include "ascii.h"
  8.  
  9. #define FALSE 0
  10. #define TRUE !FALSE
  11.  
  12. extern int GetChar();
  13. extern int PutChar();
  14.  
  15. int TxyModem(Port,Filename,Buffer,OneKflag,BatchFlag)
  16. int Port;            /* COM port [0..3] */
  17. char Filename[];     /* filename buffer */
  18. char Buffer[];       /* 1024 byte data buffer */
  19. int OneKflag;        /* if TRUE, use 1K blocks when possible */
  20. int BatchFlag;       /* if TRUE, send filename in packet 0 */
  21. {int i, k;
  22.  int Code;
  23.  int Handle;         /* file Handle */
  24.  char c;
  25.  int p;
  26.  char PacketType;
  27.  char PacketNbr;
  28.  int  PacketSize;
  29.  int  FirstPacket;
  30.  unsigned short CheckSum;
  31.  long filelength();
  32.  int Number1K = 0;       /* total # 1K packets */
  33.  int Number128 = 0;      /* total # 128 byte packets */
  34.  char NCGchar = NAK;
  35.  long FileSize;
  36.  char temp[81];
  37.  int EmptyFlag = FALSE;
  38.  /* begin */
  39.  if(BatchFlag) if(Filename[0]=='\0') EmptyFlag = TRUE;
  40.  if(!EmptyFlag)
  41.      {/* Filename is not empty */
  42.       EmptyFlag = FALSE;
  43.       Handle = open(Filename,O_RDONLY|O_BINARY,S_IREAD);
  44.       if(Handle<0)
  45.           {strcpy(temp,"Cannot open ");
  46.            strcat(temp,Filename);
  47.            DisplayLine(temp,NULL,0);
  48.            return(FALSE);
  49.           }
  50.      }
  51.  DisplayLine("XYMODEM send: waiting for Receiver ",NULL,0);
  52.  while(SioKeyPress()) SioKeyRead();
  53.  /* compute # blocks */
  54.  if(!EmptyFlag)
  55.      {FileSize = filelength(Handle);
  56.       if(OneKflag) Number1K = (int) (FileSize / 1024L);
  57.       Number128 = 1 + (int) ((FileSize -1024L*(long)Number1K -1L) / 128L);
  58.       sprintf(temp,"%d 1024 & %d 128 byte packets",Number1K,Number128);
  59.       DisplayLine(temp,NULL,0);
  60.      }
  61.  else
  62.      {/* empty file */
  63.       Number128 = 0;
  64.       Number1K = 0;
  65.       /*DisplayLine("Empty File",NULL,0);*/
  66.      }
  67.  /* clear comm port ( there may be several NAKs queued up ) */
  68.  SioRxFlush(Port);
  69.  /* get receivers start up NAK, 'C', or 'G' */
  70.  if(!TxStartup(Port,&NCGchar)) return(FALSE);
  71.  /* loop over all packets */
  72.  if(BatchFlag) FirstPacket = 0;
  73.  else FirstPacket = 1;
  74.  for(p=FirstPacket;p<=Number1K+Number128;p++)
  75.        {/* user aborts ? */
  76.         if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  77.           {TxCAN(Port);
  78.            DisplayLine("*** Canceled by USER ***",NULL,0);
  79.            return(FALSE);
  80.           }
  81.         /* issue message */
  82.         sprintf(temp,"Packet %d",p);
  83.         DisplayLine(temp,NULL,0);
  84.         /* load up Buffer */
  85.         if(p==0)
  86.               {
  87.                /* Filename packet ! */
  88.                PacketSize = 128;
  89.                k = 0;
  90.                for(i=0;i<strlen(Filename);i++) Buffer[k++] = Filename[i];
  91.                Buffer[k++] = '\0';
  92.                sprintf(temp,"%ld",FileSize);
  93.                for(i=0;i<strlen(temp);i++) Buffer[k++] = temp[i];
  94.                while(k<128) Buffer[k++] = '\0';
  95.               }
  96.         else /* p > 0 */
  97.               {/* DATA Packet: use 1K or 128 byte block ? */
  98.                if(p<=Number1K) PacketSize = 1024;
  99.                else PacketSize = 128;
  100.                /* read next block from disk */
  101.                Code = read(Handle,Buffer,PacketSize);
  102.                if(Code<=0)
  103.                      {SayError(Port,"Error on disk read");
  104.                       return(FALSE);
  105.                      }
  106.                for(i=Code;i<PacketSize;i++) Buffer[i] = 0x1a;
  107.               }
  108.         /* send this packet */
  109.         if(!TxPacket(Port,p,PacketSize,Buffer,NCGchar)) return(FALSE);
  110.         SioDelay(5);
  111.         /* must 'restart' after non null packet 0 */
  112.         if(!EmptyFlag&&(p==0)) TxStartup(Port,&NCGchar);
  113.        } /* end -- for(p) */
  114.  /* done if empty packet 0 */
  115.  if(EmptyFlag)
  116.         {DisplayLine("Batch transfer complete",NULL,0);
  117.          return(TRUE);
  118.         }
  119.  /* all done. send EOT up to 10 times */
  120.  close(Handle);
  121.  if(!TxEOT(Port))
  122.      {SayError(Port,"EOT not acknowledged");
  123.       return(FALSE);
  124.      }
  125.  DisplayLine("Transfer Complete",NULL,0);
  126.  return(TRUE);
  127. } /* end -- TxyModem */
  128.  
  129. int RxyModem(Port,Filename,Buffer,NCGchar,BatchFlag)
  130. int Port;            /* COM port [0..3] */
  131. char Filename[];     /* filename buffer */
  132. char Buffer[];       /* 1024 byte data buffer */
  133. char NCGchar;         /* NAK, 'C', or 'G' */
  134. int BatchFlag;       /* if TRUE, get filename from packet 0 */
  135. {int i;
  136.  int Handle;         /* file Handle */
  137.  int p;              /* packet index */
  138.  int Code;           /* return code */
  139.  int FirstPacket;
  140.  char PacketNbr;
  141.  int PacketSize;           /* 128 or 1024 */
  142.  long atol();
  143.  long FileSize;
  144.  char temp[81];
  145.  int  EOTflag = FALSE;
  146.  /* begin */
  147.  EOTflag = FALSE;
  148.  DisplayLine("XYMODEM Receive: Waiting for Sender ",NULL,0);
  149.  while(SioKeyPress()) SioKeyRead();
  150.  /* clear comm port */
  151.  SioRxFlush(Port);
  152.  /* Send NAKs, 'C's, or 'G's */
  153.  if(!RxStartup(Port,&NCGchar)) return(FALSE);
  154.  /* open file unless BatchFlag is on */
  155.  if(BatchFlag) FirstPacket = 0;
  156.  else
  157.      {/* start with packet 1 */
  158.       FirstPacket = 1;
  159.       /* open file passed in Filename[] for write */
  160.       Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  161.       if(Handle<0)
  162.           {strcpy(temp,"Cannot open ");
  163.            strcat(temp,Filename);
  164.            DisplayLine(temp,NULL,0);
  165.            return(FALSE);
  166.           }
  167.      }
  168.  /* get each packet in turn */
  169.  for(p=FirstPacket;;p++)
  170.      {/* user aborts ? */
  171.       if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  172.         {TxCAN(Port);
  173.          return(FALSE);
  174.         }
  175.       /* issue message */
  176.       sprintf(temp,"Packet %d",p);
  177.       DisplayLine(temp,NULL,0);
  178.       /* get next packet */
  179.       if(!RxPacket(Port,p,&PacketSize,Buffer,NCGchar,&EOTflag)) return(FALSE);
  180.       if(p==0)
  181.         {/* copy Filename */
  182.          strcpy(Filename,Buffer);
  183.          /* done if null packet 0 */
  184.          if(Filename[0]=='\0')
  185.                 {DisplayLine("Batch Transfer Complete",NULL,0);
  186.                  return(TRUE);
  187.                 }
  188.         }
  189.       /* all done if EOT was received */
  190.       if(EOTflag)
  191.           {close(Handle);
  192.            DisplayLine("Transfer Complete",NULL,0);
  193.            return(TRUE);
  194.           }
  195.       /* process packet */
  196.       if(p==0)
  197.           {/* open file using filename in packet 0 */
  198.            Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  199.            if(Handle<0)
  200.                 {strcat(Buffer," -- open failed");
  201.                  DisplayLine(Buffer,NULL,0);
  202.                  return(FALSE);
  203.                 }
  204.            /* get file length */
  205.            FileSize = atol(&Buffer[1+strlen(Buffer)]);
  206.            /* must 'restart' after packet 0 */
  207.            RxStartup(Port,&NCGchar);
  208.           }
  209.       else /* DATA packet */
  210.           {/* write Buffer */
  211.            if(BatchFlag)
  212.                {if(FileSize<(long)PacketSize) i = (int) FileSize;
  213.                 else i = PacketSize;
  214.                 i = write(Handle,Buffer,i);
  215.                 FileSize -= (long)i;
  216.                }
  217.            else write(Handle,Buffer,PacketSize);
  218.           } /* end -- else */
  219.      } /* end -- for(p) */
  220. } /* end - RxyModem */
  221.  
  222. int TxCAN(Port)
  223. int Port;
  224. {int i;
  225.  for(i=0;i<6;i++) PutChar(Port,CAN);
  226.  return(0);
  227. }